package com.hdu.metrics_center.redisCommand;


import com.hdu.metrics_center.enums.RedisCommandType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static java.util.Optional.*;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

@Component
public class RedisCommandFactory {


    private final Map<RedisCommandType, RedisCommand> redisCommandMap;

    public RedisCommandFactory(List<RedisCommand> redisCommands) {
        this.redisCommandMap = redisCommands
                .stream()
                .collect((toMap(RedisCommand::support, identity())));
    }

    public RedisCommand getRedisCommand(RedisCommandType redisCommandType) {
        return ofNullable(redisCommandMap.get(redisCommandType))
                .orElseThrow(
                        () -> new IllegalArgumentException("redisCommandType is not support : " + redisCommandType)
                );
    }

}
